<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script>
    <script type="text/javascript" src="./script/aui-slide.js"></script>
</head>

<body>
    <header class="header header-f" style="padding-top:2rem; height:4rem;">
        <div class="header-l aui-pull-left" onclick="closeWin()">
            <i class="fa fa-angle-left"></i>
        </div>
        <div class="header-title">
            购物车
        </div>
        <div class="header-r aui-pull-right" onclick="edit()">
            <span>编辑</span>
        </div>
    </header>
    <div style="height:4rem;"></div>

    <div id='list'>
      暂无订单！
        <!-- <div class="cart-list">
            <label><input class="aui-radio" type="checkbox" checked></label>
            <div class="cart-list-r">
                <img src="./images/goods-3.jpg">
                <dl>
                    <dt>智能锁指纹智能锁</dt>
                    <dd>
                        <div class="cart-list-t">规格：蓝色</div>
                        <div class="cart-list-d">
                            <span>￥1500</span>
                            <div class="goods-order-num">
                                <div class="jian"></div>
                                <input class="shu" type="text" value="1">
                                <div class="jia"></div>
                            </div>
                        </div>
                    </dd>
                </dl>
            </div>
        </div> -->

    </div>

    <script type="text/template" id="template">
        {{~it.items:value:index}}
        <div class="cart-list">
            <div class="cart-list-flex">
                <label><input class="aui-radio" type="checkbox" checked  id='check{{=index}}' onclick="election({{=index}})"></label>
                <div class="cart-list-r">
                    <img src="{{=value.image}}">
                    <dl>
                        <dt>{{=value.name}}</dt>
                        <dd>
                            {{for(var j = 0; j
                            < value.options.length; j++) { }} <div class="cart-list-t">{{=value.options[j].name}}：{{=value.options[j].value}}</div>
                {{ } }}
                <div class="cart-list-d">
                    <span>￥{{=value.price}}</span>
                    <div class="goods-order-num">
                        <div class="jian" onclick="jian({{=index}})"></div>
                        <input class="shu" type="text" id='shu{{=index}}' value="{{=value.qty}}" readonly="readonly">
                        <div class="jia" onclick="jia({{=index}})"></div>
                    </div>
                </div>
                </dd>
                </dl>
            </div>
        </div>
        <div class="cart-list-btn" style="display:none;" onclick="del({{=index}})">
            <i class="fa fa-trash-o"></i>
        </div>

        </div>
        {{~}}
    </script>



    <div class="goods-details-buy-t"></div>
    <div class="goods-order-btn">
        <label><input class="aui-radio" type="checkbox" checked  id='allcheck'  onclick="allelection()">全选</label>
        <p>
            合计金额：<span id='money'></span>
        </p>
        <button>提交</button>
    </div>


</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript">
    var delType = 0;
    var checkedType = 1;
    var data;
    var allmoney;
    var priceArr = new Array();
    apiready = function() {
        getData();
    }

    function getData() {
        var userid = $api.getStorage('userid');
        if (!userid) {
            api.openWin({
                name: 'login',
                url: './login.html',
                pageParam: {
                    name: 'test'
                }
            });

            return;
        }
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/Cart/index',
            method: 'get',
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken
            },
            data: {
                values: {
                    add_id: 1,
                    quick: 0
                },

            }
        }, function(ret, err) {
            if (ret) {
                data = ret;
                console.log(JSON.stringify(ret.result));

                if (!ret.result.cartData[0].order_price=='undefined') {
                  return;
                }
                allmoney = ret.result.cartData[0].order_price;

                for (var i = 0; i < ret.result.cartData[0].items.length; i++) {
                    priceArr.push(ret.result.cartData[0].items[i].price);
                }
                $('#money').html('￥' + ret.result.cartData[0].order_price);

                var template = $api.byId('template');
                var dotFn = doT.template(template.innerHTML);
                var html = dotFn(ret.result.cartData[0]);
                var list = $api.byId('list');
                $api.html(list, html);
            } else {
                alert(JSON.stringify(err));
            }
        });

    }

    function allelection() {
        if (checkedType == 1) {
            $("input:checkbox").removeAttr("checked");
            checkedType = 0;
            allmoney = 0;
            $('#money').html('￥' + allmoney);
        } else {
            $("input:checkbox").prop("checked", "checked");
            checkedType = 1;
            allmoney = 0;
            var t = data.result.cartData[0].items;
            for (var i = 0; i < t.length; i++) {
                var c = $('#shu' + i).val();
                allmoney = allmoney + parseFloat(t[i].price) * parseInt(c);
            }
            $('#money').html('￥' + allmoney);
        }
    }

    function election(index) {
        var order = data.result.cartData[0];
        var temp = $("#shu" + index).val();
        temp = parseInt(temp);

        if ($('#check' + index).is(':checked')) {　
            allmoney = parseFloat(allmoney) + parseFloat(priceArr[index]) * temp;
        } else {
            allmoney = parseFloat(allmoney) - parseFloat(priceArr[index]) * temp;

        }
        $('#money').html('￥' + allmoney);

    }

    function jia(index) {
        var temp = $("#shu" + index).val();
        temp = parseInt(temp) + 1;
        $("#shu" + index).val(temp);
        //总额
        allmoney = parseFloat(allmoney) + parseFloat(priceArr[index]);
        $('#money').html('￥' + allmoney);

    }

    function jian(index) {
        if ($("#shu" + index).val() <= 1) {} else {
            var temp = $("#shu" + index).val();
            temp = parseInt(temp) - 1;
            $("#shu" + index).val(temp);
            allmoney = parseFloat(allmoney) - parseFloat(priceArr[index]);
            $('#money').html('￥' + allmoney);
        }
    }


    function edit() {
        if (delType == 0) {
            $('.cart-list-btn').show();
            delType = 1;
        } else {
            $('.cart-list-btn').hide();
            delType = 0;
        }
    }

    function del(index) {
        api.confirm({
            title: '确定要移除该商品么？',
            // msg: 'testmsg',
            buttons: ['确定', '取消']
        }, function(ret, err) {
            var d = ret.buttonIndex;
            if (d == 1) {
                var ids = data.result.cartData[0].ids;
                var userid = $api.getStorage('userid');
                var authtoken = $api.getStorage('authtoken');
                var idArr = ids.split(',');
                api.ajax({
                    url: webAddress + '/a/shop/Cart/del',
                    method: 'post',
                    headers: {
                        TOKEN: token,
                        authuid: userid,
                        authtoken: authtoken
                    },
                    data: {
                        values: {
                            rowid: idArr[index]
                        },

                    }
                }, function(ret, err) {
                    if (ret) {
                        alert(JSON.stringify(ret));
                    } else {
                        alert(JSON.stringify(err));
                    }
                });



            }

        });
    }




    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
